var __UIStoryMode_instance = null;
function getUIStoryMode() { return __UIStoryMode_instance; }


// ps:因为夸类,所以得全局
// 联赛ID
var LIANSAI_IDS = [];
// 联赛队伍ID
var LIANSAI_TEAM_IDS = [];


// 联赛选择
var TVLiansaiCell = cc.Node.extend({

    setIndexOfCells:function(index) {
        this.removeAllChildren();

        var lsid = LIANSAI_IDS[index];
        var d = DataManager.storyDataLiansai[lsid];

        //glb.printObject(d);
        /*
        "map": "leagueMap/LeagueMap60003Asset.png",
        "wpid": [
          {
            "powercurr": 24.4557894737,
            "name": "霍伊莱特",
            "color": 16515327,
            "powermax": "122.3",
            "id": "6600",
            "icon": "28000482"
          },
          {
            "powercurr": 24.4,
            "name": "库克",
            "color": 16515327,
            "powermax": "122",
            "id": "6602",
            "icon": "1511783"
          },
          {
            "powercurr": 24.7978947368,
            "name": "布罗基",
            "color": 16515327,
            "powermax": "124",
            "id": "6596",
            "icon": "706418"
          }
        ],
        "pid": "60002",
        "cupid": "50028",
        "lname": "J联赛",
        "team": [
          "19",
          "20",
          "21",
          "22",
          "23",
          "24",
          "25",
          "26",
          "27",
          "28"
        ],
        "y": 0,
        "x": 0,
        "desc": "J联赛，几乎与中国职业联赛同时开始，但经过多年经营，逐渐成为亚洲知名度和水平最高的职业联赛之一。",
        "id": "60003",
        "icon": "leagueLogo/event_japan.png"
        */

        // 背景
        var bg = cc.Sprite.create('res/UI/UIStoryMode/img_icon_bg1_01.png');
        bg.setScale(0.5);
        bg.setAnchorPoint(cc.p(0,0));
        bg.setPosition(cc.p(0,0));
        this.addChild(bg);

        // 图标
        var image = 'res/Misc/lock.png';
        if (d.icon) { image = 'res/'+d.icon; }

        var sprite = cc.Sprite.create(image);
        sprite.setScale(0.47);
        sprite.setAnchorPoint(cc.p(0,0));
        sprite.setPosition(cc.p(10,15));
        this.addChild(sprite);

        // 名字
        var label = cc.LabelTTF.create(d.lname, "Helvetica", 20.0);
        label.setPosition(cc.p(25, 5));
        label.setAnchorPoint(cc.p(0.5,0));
        label.setScale(0.5);
        this.addChild(label);
    },
});

// 联赛 队伍选择
var TVLiansaiTeamCell = cc.Node.extend({

    setIndexOfCells:function(index) {
        this.removeAllChildren();

        var tid = LIANSAI_TEAM_IDS[index];
        var d = DataManager.storyDataLiansaiTeam[tid];
        /*
        "1": {
        "wid": ["50001"],
        "name": "大连实德",
        "power": 11.9995215311,
        "y": "42",
        "tid": "1",
        "x": "527",
        "desc": "成立于2000年（前身为1994年成立的大连万达队），中国顶级联赛传统四大豪门之一，曾八夺中国顶级联赛冠军，并多次代表中国参加亚冠联赛（包括其前身亚俱杯），取得亚俱杯亚军和亚冠四强的骄人战绩。",
        "wdesc": "资金，经验"
        },
        */
        // 背景
        var bg = cc.Sprite.create('res/UI/UIStoryMode/img_icon_bg2_01.png');
        bg.setScale(0.5);
        bg.setAnchorPoint(cc.p(0,0));
        bg.setPosition(cc.p(0,0));
        this.addChild(bg);

        // 图标
        var sprite = cc.Sprite.create('res/icon/badge/club_' + d.tid + '_normal.png');
        sprite.setScale(0.33);
        sprite.setAnchorPoint(cc.p(0,0));
        sprite.setPosition(cc.p(10,8));
        this.addChild(sprite);
        
        // 名字
        var label = cc.LabelTTF.create(d.name, "Helvetica", 20.0);
        label.setPosition(cc.p(51,25));
        label.setAnchorPoint(cc.p(0,0));
        label.setScale(0.5);
        this.addChild(label);

        // 战斗力
        var label = cc.LabelTTF.create('战斗力:' + d.power.toFixed(1), "Helvetica", 20.0);
        label.setPosition(cc.p(51,12));
        label.setAnchorPoint(cc.p(0,0));
        label.setScale(0.5);
        this.addChild(label);
    },
});

var UIStoryMode = function(){};
UIStoryMode.prototype.onDidLoadFromCCB = function()
{
    cc.log("UIStoryMode.onDidLoadFromCCB");
    __UIStoryMode_instance = this;

    // 当前选中球队
    this._cur_team = -1;

    // 分页按钮
    this._tab = cc.Menu.create();
    this._tab.setAnchorPoint(cc.p(0,0));
    this._tab.setPosition(cc.p(0,0));
    this.uiWindow.addChild(this._tab);

    this._tabBtn1 = cc.MenuItemToggle.create(
        cc.MenuItemImage.create('res/UI/UIStoryMode/btn_liansai02.png', 'res/UI/UIStoryMode/btn_liansai02.png'),
        cc.MenuItemImage.create('res/UI/UIStoryMode/btn_liansai01.png', 'res/UI/UIStoryMode/btn_liansai01.png'),
        this.onTabBtn1, this
    );
    this._tabBtn1.setScale(0.5);
    this._tabBtn1.setPosition(cc.p(47,288));
    this._tab.addChild(this._tabBtn1);

    this._tabBtn2 = cc.MenuItemToggle.create(
        cc.MenuItemImage.create('res/UI/UIStoryMode/btn_beisai02.png', 'res/UI/UIStoryMode/btn_beisai02.png'), 
        cc.MenuItemImage.create('res/UI/UIStoryMode/btn_beisai01.png', 'res/UI/UIStoryMode/btn_beisai01.png'),
        this.onTabBtn2, this
    );
    this._tabBtn2.setScale(0.5);
    this._tabBtn2.setPosition(cc.p(116,288));
    this._tab.addChild(this._tabBtn2);

    this._tabBtn1.setSelectedIndex(0);
    this._tabBtn2.setSelectedIndex(1);

    // 联赛 杯赛 分页
    this._layer1 = cc.Layer.create();
    this.uiWindow.addChild(this._layer1);
    this._layer2 = cc.Layer.create();
    this.uiWindow.addChild(this._layer2);

    // 联赛信息
    this.showLiansaiInfo(null);

    // 请求网络
    gnet.sendFB('CP', 'CONF', '0', _func);
    function _func(data) {
        DataManager.storyDataLiansai = data;
        getUIStoryMode().updateData();
    };

};

UIStoryMode.prototype.updateData = function()
{
    cc.log('UIStoryMode.updateData');
    // 清理
    if(this._tvLiansai) { this._layer1.removeChild(this._tvLiansai, true); this._tvLiansai = null; }
    if(this._tvLiansaiTeam) { this._layer1.removeChild(this._tvLiansaiTeam, true); this._tvLiansaiTeam = null; }

    var d = DataManager.storyDataLiansai;

    // 联赛界面
    LIANSAI_IDS = [];
    for (var k in d) {
        LIANSAI_IDS.push(k);
    }
    LIANSAI_IDS.sort();
    LIANSAI_IDS.reverse();

    // 联赛选择
    this._tvLiansai = TableViewH.create(
        cc.SCROLLVIEW_DIRECTION_VERTICAL, cc.size(50, 169), 
        cc.size(50, 50), LIANSAI_IDS.length, TVLiansaiCell, 
        this.onClickLiansai );
    this._tvLiansai.setPosition(cc.p(6,80));
    this._layer1.addChild(this._tvLiansai);
};

UIStoryMode.prototype.onClickLiansai = function(index) {
    var lsid = LIANSAI_IDS[index];
    var d = DataManager.storyDataLiansai[lsid];
    getUIStoryMode().showLiansaiInfo(d, false);

    // 请求网络
    gnet.sendFB('CP', 'LCONF', lsid.toString(), _func);
    function _func(data) {
        DataManager.storyDataLiansaiTeam = data;
        getUIStoryMode().updateLiansaiTeam(data);
    };
};


UIStoryMode.prototype.onClickLiansaiTeam = function(index) {
    // 选中球队
    var tid = LIANSAI_TEAM_IDS[index];
    var d = DataManager.storyDataLiansaiTeam[tid];
    cc.log('选中球队:'+d.name+' '+tid);

    getUIStoryMode().showLiansaiInfo(d, true);
};


// 更新联赛队伍
UIStoryMode.prototype.updateLiansaiTeam = function(data)
{
    // 清理
    if(this._tvLiansaiTeam) { this._layer1.removeChild(this._tvLiansaiTeam, true); this._tvLiansaiTeam = null; }

    // 联赛界面
    LIANSAI_TEAM_IDS = [];
    for (var k in data) {
        LIANSAI_TEAM_IDS.push(k);
    }
    LIANSAI_TEAM_IDS.sort();
    LIANSAI_TEAM_IDS.reverse();

    // 联赛队伍选择
    this._tvLiansaiTeam = TableViewH.create(
        cc.SCROLLVIEW_DIRECTION_HORIZONTAL, cc.size(316, 50), 
        cc.size(110, 50), LIANSAI_TEAM_IDS.length, TVLiansaiTeamCell, 
        this.onClickLiansaiTeam );
    this._tvLiansaiTeam.setPosition(cc.p(24,7));
    this._layer1.addChild(this._tvLiansaiTeam);
};


// 显示联赛信息
UIStoryMode.prototype.showLiansaiInfo = function(data, isTeam) {
    if (!data) {
        this.uiLiansaiInfoPanel.setVisible(false);
        this.uiStart.setEnabled(false);
        return;
    }

    // 显示
    this.uiLiansaiInfoPanel.setVisible(true);
    this.uiLiansaiReward.setVisible(!isTeam);

    var d = data;
    if (!isTeam) {
        // 联赛
        this.uiLiansaiName.setString(d.lname);
        this.uiLiansaiState.setString(' ');
        this.uiLiansaiDesc.setString(d.desc);    
        // 奖励球员
        var path = 'res/icon/player/'
        this.uiReward1Image.init(path + 'player_' + d.wpid[0].id + '_normal.png');
        this.uiReward2Image.init(path + 'player_' + d.wpid[1].id + '_normal.png');
        this.uiReward3Image.init(path + 'player_' + d.wpid[2].id + '_normal.png');

        this.uiReward1Name.setString(d.wpid[0].name);
        this.uiReward1Name.setColor(glb.int2color(d.wpid[0].color));
        this.uiReward2Name.setString(d.wpid[1].name);
        this.uiReward2Name.setColor(glb.int2color(d.wpid[1].color));
        this.uiReward3Name.setString(d.wpid[2].name);
        this.uiReward3Name.setColor(glb.int2color(d.wpid[2].color));

    } else {
        // 联赛球队
        this.uiLiansaiName.setString(d.name);
        this.uiLiansaiState.setString(' ');
        this.uiLiansaiDesc.setString(d.desc);
    }

    // 开始战斗
    if(isTeam) {
        this._cur_team = d.tid;
    } else {
        this._cur_team = -1;
    }
    this.uiStart.setEnabled(isTeam);

};

// 关闭前清理
UIStoryMode.prototype.clearBeforeClose = function()
{
    if(this._tvLiansai) { this._layer1.removeChild(this._tvLiansai, true); this._tvLiansai = null; }
    if(this._tvLiansaiTeam) { this._layer1.removeChild(this._tvLiansaiTeam, true); this._tvLiansaiTeam = null; }
};

// 关闭按钮
UIStoryMode.prototype.onClose = function()
{
    getUIMain().closeUIStoryMode();
};
// 滚动翻页按钮 上 下 左 右
UIStoryMode.prototype.onUp = function()
{
    this._tvLiansai.moveContentInDuration(cc.p(0,-50), 0.5);
};
UIStoryMode.prototype.onDown = function()
{
    this._tvLiansai.moveContentInDuration(cc.p(0,50), 0.5);
};
UIStoryMode.prototype.onLeft = function()
{
    if(this._tvLiansaiTeam) this._tvLiansaiTeam.moveContentInDuration(cc.p(110,0), 0.5);
};
UIStoryMode.prototype.onRight = function()
{
    if(this._tvLiansaiTeam) this._tvLiansaiTeam.moveContentInDuration(cc.p(-110,0), 0.5);
};

// 切换Tab1
UIStoryMode.prototype.onTabBtn1 = function(item)
{
    if(this._tabBtn1.getSelectedIndex() == 0) {
        this._layer1.setVisible(true);
        this._layer2.setVisible(false);
        this.uiLiansai.setVisible(true);
        this.uiBeisai.setVisible(false);
    }
    this._tabBtn1.setSelectedIndex(0);
    this._tabBtn2.setSelectedIndex(1);
};

// 切换Tab2
UIStoryMode.prototype.onTabBtn2 = function(item)
{
    if(this._tabBtn2.getSelectedIndex() == 0) {
        /*
        this._layer1.setVisible(false);
        this._layer2.setVisible(true);
        this.uiLiansai.setVisible(false);
        this.uiBeisai.setVisible(true);
        */
    }
    this._tabBtn1.setSelectedIndex(1);
    this._tabBtn2.setSelectedIndex(0);
};

// 开始战斗
UIStoryMode.prototype.onStart = function()
{
    cc.log('onStart');
    gnet.sendFB('CP', 'PK', this._cur_team.toString(), null);
};
